package com.yocard.yxyd.ui.presenter;

import com.yocard.yxyd.utils.util.StringUtils;
import com.yocard.yxyd.base.CommonSubscriber;
import com.yocard.yxyd.base.RxPresenter;
import com.yocard.yxyd.http.HttpManager;
import com.yocard.yxyd.ui.contract.VerifyContract;
import com.yocard.yxyd.utils.RxUtil;

import org.json.JSONArray;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.inject.Inject;

/**
 * Created on 2019/4/18.
 */
public class VerifyPresenter extends RxPresenter<VerifyContract.View> implements VerifyContract.Presenter {

    @Inject
    public VerifyPresenter(HttpManager manager) {
        super(manager);
    }


    @Override
    public String splitMobile(String mobile) {
        if(StringUtils.isEmpty(mobile)){
            return "";
        }
        String[] split = mobile.split("");
        List<String> tempList = new ArrayList<>(Arrays.asList(split));
        StringBuilder sb = new StringBuilder();
        int i;
        for (i = 0; i < tempList.size(); i++) {
            if (i == 4) {
                sb.append(" ");
                sb.append(tempList.get(i));
            } else if (i == 8) {
                sb.append(" ");
                sb.append(tempList.get(i));
            } else {
                sb.append(tempList.get(i));
            }
        }
        return sb.toString();
    }

    @Override
    public void getOldMobileCode() {
        addSubscribe(httpManager.getOldMObileCode().compose(RxUtil.rxSchedulerHelper())
                .subscribeWith(new CommonSubscriber<List<Object>>(mView) {
                    @Override
                    public void onSuccess(List<Object> data) {
                        mView.onGetOldSuccess();
                    }
                }));
    }

    @Override
    public void verifyOldCode(String code) {
        addSubscribe(httpManager.pushOldcode(code).compose(RxUtil.rxSchedulerHelper())
                .subscribeWith(new CommonSubscriber<List<Object>>(mView) {
                    @Override
                    public void onSuccess(List<Object> data) {
                        mView.onOldCodePush();
                    }
                }));
    }
}
